﻿Imports Persistencia
Public Class ctrOrdemServico
    Inherits ctrPadrao

#Region "Declaração de variáveis"

    Private objOrdemServico As New perOrdemServico
    Private prvIDOrdem As Int32

#End Region

#Region "Propriedades"

    Public Property IDOrdem() As Int32
        Get
            Return Me.prvIDOrdem
        End Get
        Set(ByVal value As Int32)
            Me.prvIDOrdem = value
        End Set
    End Property

    Public ReadOnly Property sqlConsulta() As String
        Get
            Return Me.objOrdemServico.sqlConsulta
        End Get
    End Property

#End Region

#Region "Métodos públicos"

    Public Function salvar(ByVal iIDOrdem As Int32, _
                           ByVal iIDCliente As Int32, _
                           ByVal iIDContato As Int32, _
                           ByVal iIDUsuario As Int32, _
                           ByVal dtEntrada As Date, _
                           ByVal dtSaida As Date, _
                           ByVal dtConclusao As Date, _
                           ByVal bConcluido As Boolean, _
                           ByVal NumOS As String) As Boolean

        Dim bResultado As Boolean = False
        Dim dtDataConclusao As Date = Date.MinValue

        If (bConcluido) Then
            dtDataConclusao = dtConclusao
        End If

        If iIDOrdem > 0 Then
            Me.prvIDOrdem = iIDOrdem
            Call objOrdemServico.alterarOrdemServico(iIDOrdem, iIDCliente, iIDContato, iIDUsuario, _
                                                     dtEntrada, dtSaida, dtConclusao, NumOS)
            bResultado = True
        Else
            Me.prvIDOrdem = objOrdemServico.inserirOS(iIDCliente, _
                                                      iIDContato, _
                                                      iIDUsuario, _
                                                      dtEntrada, _
                                                      dtSaida, _
                                                      dtDataConclusao, NumOS)

            bResultado = True
        End If

        Return bResultado

    End Function

    Public Function excluirOrdem(ByVal iIDOrdem As Int32) As Boolean
        Dim bRetorno As Boolean = True
        Try
            Me.objOrdemServico.excluirOrdemServico(iIDOrdem)

        Catch ex As Exception
            bRetorno = False
            Throw New Exception("Exceção no método 'excluirOrdem'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        Finally
            excluirOrdem = bRetorno
        End Try

    End Function
    Public Function selecionarOrdem(ByVal iIDOrdem As Int32) As DataRow
        Try
            Return objOrdemServico.selecionarOrdem(iIDOrdem)

        Catch ex As Exception

            Throw New Exception("Exceção no método 'selecionarStatus'. " & Environment.NewLine & Environment.NewLine & ex.Message)

        End Try

    End Function

    Public Function retornarCampoOrdem(ByVal sCampo As String, _
                                        ByVal iIDOrdem As Int32) As DataRow

        Try
            Dim dtDados As New DataTable

            dtDados = Me.objOrdemServico.selecionarCampoOrdem(sCampo, iIDOrdem)

            If Not dtDados Is Nothing Then
                Return dtDados.Rows(0)
            Else
                Return Nothing
            End If

        Catch ex As Exception

            Throw New Exception("Exceção no método 'retornarCampoOrdem'. " & Environment.NewLine & Environment.NewLine & ex.Message)

        End Try

    End Function

#End Region

End Class
